﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ANGFLib;

namespace ANGF
{
    public partial class FormEditPermaTalker : Form
    {
        private MyXmlDoc doc;
        public FormEditPermaTalker(MyXmlDoc doc)
        {
            this.doc = doc;
            InitializeComponent();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(comboBoxShortName.Text))
            {
                MessageBox.Show(this, "短い名前を入力してください。", this.Text);
                return;
            }
            if (comboBoxShortName.Text.Any(c => Char.IsWhiteSpace(c)))
            {
                MessageBox.Show(this, "短い名前に空白文字を含めることはできません。", this.Text);
                return;
            }
            DialogResult = DialogResult.OK;
            Close();
        }

        private void FormEditPermaTalker_Load(object sender, EventArgs e)
        {
            comboBoxShortName.Items.Clear();
            for (char i = 'a'; i <= 'z'; i++) comboBoxShortName.Items.Add(i);
            comboBoxShortName.SelectedIndex = 0;

            comboBoxTarget.Items.Clear();
            foreach (var n in General.GetCandidatePersonList(doc))
            {
                comboBoxTarget.Items.Add(n);
            }
            if (comboBoxTarget.Items.Count > 0) comboBoxTarget.SelectedIndex = 0;
        }

        private void comboBoxTarget_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0) return;
            e.DrawBackground();
            var item = (Person)this.comboBoxTarget.Items[e.Index];
            string s = item.HumanReadableName + " (" + item.Id + ")";
            e.Graphics.DrawString(s, e.Font, new SolidBrush(e.ForeColor), e.Bounds);
            e.DrawFocusRectangle();
        }

        internal string GetResultText()
        {
            if (comboBoxTarget.SelectedIndex < 0) return "";
            var target = (Person)comboBoxTarget.Items[comboBoxTarget.SelectedIndex];
            return "#P " + comboBoxShortName.Text + " " + target.Id + "//" + target.HumanReadableName + "\r\n";
        }
    }
}
